<?php

namespace api\modules\v1\models;
use api\func\func;
use Yii;
use api\common\models\Common;
use yii\data\Pagination;
class Balancewater extends Common
{
    public static function tableName()
    {
        return '{{%user_balance_water}}';
    }


    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'uid' => Yii::t('app', '用户id'),
            'type' => Yii::t('app', '流水类型 1:打赏'),
            'order_num' => Yii::t('app', '订单号'),
            'balance_type' => Yii::t('app', '流水类型 1:加钱 2:减钱'),
            'balance' => Yii::t('app', '流水金额'),
            'end_balance' => Yii::t('app', '余额'),
            'json' => Yii::t('app', '相关json数据'),
            'time' => Yii::t('app', '时间'),
        ];
    }

    /*
     * 积分流水插入
     * @param $uid 用户id
     * @param $type 流水类型 1:打赏 2:被打赏 3:余额提现
     * @param $pay_type 金额变动渠道 1:支付宝 2:微信 3:余额
     * @param $order_num 订单号
     * @param $balance_type 流水类型 1:加钱 2:减钱
     * @param $balance 流水金额
     * @param $end_balance 余额
     * @param $msg 详细信息
     * @param $json 相关json数据
     */
    public function add($uid,$bid,$type,$pay_type,$order_num,$balance_type,$balance,$end_balance,$msg,$json){
        $this->uid = $uid;//用户id
        $this->bid = $bid;//对方用户id
        $this->type = $type;//流水类型 1:打赏 2:被打赏 3:余额提现
        $this->pay_type = $pay_type;//金额变动渠道 1:支付宝 2:微信 3:余额
        $this->order_num = $order_num;//订单号
        $this->balance_type = $balance_type;//流水类型 1:加钱 2:减钱
        $this->balance = $balance;//流水金额
        $this->end_balance = $end_balance;//余额
        $this->msg = $msg;//详细信息
        $this->json = $json;//相关json数据
        $this->time = date('Y-m-d H:i:s');//时间
        $this->isNewRecord= true;
        return $this->save();
    }

    /*
     * 收益总览
     */
    public function getBalancewater(){
        $query = (new \yii\db\Query())
            ->select(['b.nickname','b.phone','a.id','a.type','a.pay_type','a.balance_type','a.balance','a.time','a.json'])
            ->from('{{%user_balance_water}} as a')
            ->join('LEFT JOIN','{{%user_app}} as b','a.bid=b.id')
            ->where(['a.uid'=>Yii::$app->user->identity->id]);

        $count = $query->count();
        $pagination = new Pagination(['totalCount' => $count]);
        $pagination->defaultPageSize = Yii::$app->params['page_size'];
        $data_tmp = $query->offset($pagination->offset)
            ->limit($pagination->limit)
            ->orderBy('time desc')
            ->all();
        $this->serializePagination($pagination);
        $_data = [];
        $data = [];
        foreach($data_tmp as $key=> $value){
            $data[$key]['type'] = $value['type'];//流水类型 1:打赏 2:被打赏 3:余额提现
            $data[$key]['pay_type'] = $value['pay_type'];//金额变动渠道 1:支付宝 2:微信 3:余额
            $data[$key]['nickname'] = $value['nickname'] != '' ? $value['nickname'] : func::replace_phone($value['phone']);
            $data[$key]['balance_type'] = $value['balance_type'];//流水类型 1:加钱 2:减钱
            $data[$key]['balance'] = $value['balance'];//流水金额
            $data[$key]['time'] = $value['time'];//流水时间
            $data[$key]['json'] = (object)[];
            if($value['type'] == 3){
                $data[$key]['json'] = json_decode($value['json']);//流水时间
            }
        }
        $_data['items'] = $data;
        $_data['_links'] = $this->linksEnvelope;
        $_data['_meta'] = $this->metaEnvelope;
        return $_data;
    }

}
